package com.ysd.lis.service.bac.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacBloodCultrue;
import com.ysd.lis.entity.bac.BacCultureMedium;
import com.ysd.lis.entity.bac.BacStrain;
import com.ysd.lis.entity.bac.BacTestCultureMedium;
import com.ysd.lis.mapper.bac.BacBloodCultrueMapper;
import com.ysd.lis.mapper.bac.BacTestCultureMediumMapper;
import com.ysd.lis.request.bac.BloodCulMediumParam;
import com.ysd.lis.request.bac.DeleteAllBacTestCultuMediumParam;
import com.ysd.lis.service.bac.BacTestCultureMediumService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 微生物检验应用培养基 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-04-11
 */
@Service
public class BacTestCultureMediumServiceImpl extends ServiceImpl<BacTestCultureMediumMapper, BacTestCultureMedium> implements BacTestCultureMediumService {
    @Autowired
    BacTestCultureMediumMapper bacTestCultureMediumMapper;

    @Override
    public Result findBacTestCultureMediumList(BacTestCultureMedium bacTestCultureMedium) {
        List<BacTestCultureMedium> list = findBacTestCultureMediumArray(bacTestCultureMedium);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<BacTestCultureMedium> findBacTestCultureMediumArray(BacTestCultureMedium bacTestCultureMedium) {
        MPJLambdaWrapper<BacTestCultureMedium> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacTestCultureMedium.class);
        queryWrapper.selectAs("tb1", BacCultureMedium::getMinCollection, BacTestCultureMedium::getMinBloodVal);
        queryWrapper.selectAs("tb1", BacCultureMedium::getWeight, BacTestCultureMedium::getCultuMedWeight);
        queryWrapper.selectAs("tb1", BacCultureMedium::getContainerBarcodeHead, BacTestCultureMedium::getCultuMedBarCodePrefix);
        queryWrapper.selectAs("tb1", BacCultureMedium::getContainerBarcodeLength, BacTestCultureMedium::getCultuMedBarCodeLength);
        queryWrapper.selectAs("tb1", BacCultureMedium::getAutoWindowScan, BacTestCultureMedium::getIsAutoPopup);
        queryWrapper.selectAs("tb1", BacCultureMedium::getMediumTag, BacTestCultureMedium::getMediumTag);
        queryWrapper.selectAs("tb1", BacCultureMedium::getTagsNum, BacTestCultureMedium::getTagsNum);
        queryWrapper.selectAs("tb1", BacCultureMedium::getIsPrintLabel, BacTestCultureMedium::getIsPrintLabels);

        queryWrapper.eq(BacTestCultureMedium::getDelFlag, 0);
//        queryWrapper.eq(StringUtils.isNotBlank(bacTestCultureMedium.getMaininfoId()), BacTestCultureMedium::getMaininfoId, bacTestCultureMedium.getMaininfoId());
        //queryWrapper.eq(BeanUtil.isNotEmpty(bacTestCultureMedium.getSampleda()), BacTestCultureMedium::getSampleda, bacTestCultureMedium.getSampleda());
        if (BeanUtil.isNotEmpty(bacTestCultureMedium.getSampleda())) {
            Timestamp timestamp = new Timestamp(bacTestCultureMedium.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            queryWrapper.ge(BacStrain::getSampleda, startTime);
            queryWrapper.le(BacStrain::getSampleda, endTime);
        }
        queryWrapper.eq(StrUtil.isNotEmpty(bacTestCultureMedium.getSampleNo()), BacTestCultureMedium::getSampleNo, bacTestCultureMedium.getSampleNo());
        queryWrapper.eq(StrUtil.isNotEmpty(bacTestCultureMedium.getRptGroup()), BacTestCultureMedium::getRptGroup, bacTestCultureMedium.getRptGroup());


        queryWrapper.eq(StringUtils.isNotBlank(bacTestCultureMedium.getReqNo()), BacTestCultureMedium::getReqNo, bacTestCultureMedium.getReqNo());
//        queryWrapper.eq(StringUtils.isNotBlank(bacTestCultureMedium.getReqMainId()), BacTestCultureMedium::getReqMainId, bacTestCultureMedium.getReqMainId());
//        queryWrapper.eq(StringUtils.isNotBlank(bacTestCultureMedium.getCultureId()), BacTestCultureMedium::getCultureId, bacTestCultureMedium.getCultureId());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestCultureMedium.getCultureNo()), BacTestCultureMedium::getCultureNo, bacTestCultureMedium.getCultureNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestCultureMedium.getCultureNa()), BacTestCultureMedium::getCultureNa, bacTestCultureMedium.getCultureNa());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestCultureMedium.getTestFlowNo()), BacTestCultureMedium::getTestFlowNo, bacTestCultureMedium.getTestFlowNo());
//        queryWrapper.eq(StringUtils.isNotBlank(bacTestCultureMedium.getCultureCombinId()), BacTestCultureMedium::getCultureCombinId, bacTestCultureMedium.getCultureCombinId());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestCultureMedium.getCultureCombinNo()), BacTestCultureMedium::getCultureCombinNo, bacTestCultureMedium.getCultureCombinNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestCultureMedium.getCultureCombinNa()), BacTestCultureMedium::getCultureCombinNa, bacTestCultureMedium.getCultureCombinNa());

        queryWrapper.leftJoin(BacCultureMedium.class, "tb1", BacCultureMedium::getCode, BacTestCultureMedium::getCultureNo);

        return bacTestCultureMediumMapper.selectList(queryWrapper);
    }

    @Override
    public Result addBacTestCultureMedium(BacTestCultureMedium bacTestCultureMedium) {
        int i = bacTestCultureMediumMapper.insert(bacTestCultureMedium);
        if (i > 0) {
            return Result.succ(1, "新增成功", bacTestCultureMedium);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result deleteBacTestCultureMedium(BacTestCultureMedium bacTestCultureMedium) {
        bacTestCultureMedium.setDelFlag(1);
        int i = bacTestCultureMediumMapper.updateById(bacTestCultureMedium);
        if (i > 0) {
            return Result.succ(1, "删除成功", bacTestCultureMedium);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result updateBacTestCultureMedium(BacTestCultureMedium bacTestCultureMedium) {
        int i = bacTestCultureMediumMapper.updateById(bacTestCultureMedium);
        if (i > 0) {
            return Result.succ(1, "更新成功", bacTestCultureMedium);
        }
        return Result.fail("更新失败");
    }

    /*批量新增培养基*/
    @Override
    public Result batchInsertBacTestCultureMedium(List<BacTestCultureMedium> bacTestCultureMediumList) {
        boolean r = this.saveBatch(bacTestCultureMediumList);
        return r ? Result.succ(1, "", bacTestCultureMediumList) : Result.fail("操作失败");
    }

    /*批量删除*/
    @Override
    public Result allDeleteBacTestCultureMedium(DeleteAllBacTestCultuMediumParam input) {
        if (StringUtils.isBlank(input.getReqMainId())) {
            return Result.succ(1, "操作成功", null);
        }
        MPJLambdaWrapper<BacTestCultureMedium> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacTestCultureMedium::getDelFlag, 0);
        queryWrapper.eq(BacTestCultureMedium::getReqNo, input.getReqNo());
        int i = bacTestCultureMediumMapper.delete(queryWrapper);
        return i > 0 ? Result.succ(1, "删除成功", null) : Result.fail("操作失败");
    }

    /*批量更新*/
    @Override
    public Result batchUpdateBacTestCultureMedium(List<BacTestCultureMedium> bacTestCultureMediumList) {
        boolean r = this.updateBatchById(bacTestCultureMediumList);
        return r ? Result.succ(1, "操作成功", bacTestCultureMediumList) : Result.fail("批量更新失败");
    }

    /*获取血培养用培养基*/
    @Override
    public Result findBloodCulMediumList(BloodCulMediumParam input) {
        MPJLambdaWrapper<BacTestCultureMedium> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacTestCultureMedium.class);
        queryWrapper.selectAs("tb1", BacCultureMedium::getMinCollection, BacTestCultureMedium::getMinBloodVal);
        queryWrapper.selectAs("tb1", BacCultureMedium::getWeight, BacTestCultureMedium::getCultuMedWeight);
        queryWrapper.selectAs("tb1", BacCultureMedium::getContainerBarcodeHead, BacTestCultureMedium::getCultuMedBarCodePrefix);
        queryWrapper.selectAs("tb1", BacCultureMedium::getContainerBarcodeLength, BacTestCultureMedium::getCultuMedBarCodeLength);
        queryWrapper.selectAs("tb1", BacCultureMedium::getMediumType, BacTestCultureMedium::getMediumType);
        queryWrapper.selectAs("tb1", BacCultureMedium::getMediumColor, BacTestCultureMedium::getMediumColor);
        queryWrapper.selectAs("tb1", BacCultureMedium::getMediumTag, BacTestCultureMedium::getMediumTag);
        queryWrapper.selectAs("tb1", BacCultureMedium::getCultureBottle, BacTestCultureMedium::getCultureBottle);
        queryWrapper.and(p -> p.isNull(BacTestCultureMedium::getParentId).or().eq(BacTestCultureMedium::getParentId, ""));
        queryWrapper.eq(BacTestCultureMedium::getDelFlag, 0);
        if (BeanUtil.isNotEmpty(input.getSampleda())) {
            Timestamp timestamp = new Timestamp(input.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            queryWrapper.ge(BacTestCultureMedium::getSampleda, startTime);
            queryWrapper.le(BacTestCultureMedium::getSampleda, endTime);
        }
        queryWrapper.eq(StrUtil.isNotEmpty(input.getSampleNo()), BacTestCultureMedium::getSampleNo, input.getSampleNo());
        queryWrapper.eq(StrUtil.isNotEmpty(input.getRptGroup()), BacTestCultureMedium::getRptGroup, input.getRptGroup());
        queryWrapper.eq(StringUtils.isNotBlank(input.getReqNo()), BacTestCultureMedium::getReqNo, input.getReqNo());
        queryWrapper.eq(StringUtils.isNotBlank(input.getTestFlowNo()), BacTestCultureMedium::getTestFlowNo, input.getTestFlowNo());
        queryWrapper.leftJoin(BacCultureMedium.class, "tb1", p -> p.eq(BacCultureMedium::getCode, BacTestCultureMedium::getCultureNo).eq(BacCultureMedium::getBloodFlag, 1));

        List<BacTestCultureMedium> resList = bacTestCultureMediumMapper.selectList(queryWrapper);


        if (resList.isEmpty()) {
            return Result.succ(1, "成功", new ArrayList<>());
        }
        //查询BAC_BLOOD_CULTRUE 表
        List<String> cultureNoList = resList.stream().map(BacTestCultureMedium::getCultureNo).collect(Collectors.toList());

        MPJLambdaWrapper<BacBloodCultrue> queryWrapper1 = new MPJLambdaWrapper<>();
        queryWrapper1.eq(BacBloodCultrue::getDelFlag, 0);
        if (BeanUtil.isNotEmpty(input.getSampleda())) {
            Timestamp timestamp = new Timestamp(input.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            queryWrapper1.ge(BacBloodCultrue::getSampleda, startTime);
            queryWrapper1.le(BacBloodCultrue::getSampleda, endTime);
        }
        queryWrapper1.eq(StrUtil.isNotEmpty(input.getSampleNo()), BacBloodCultrue::getSampleNo, input.getSampleNo());
        queryWrapper1.eq(StrUtil.isNotEmpty(input.getRptGroup()), BacBloodCultrue::getRptGroup, input.getRptGroup());
        queryWrapper1.in(BacBloodCultrue::getCulMediumNo, cultureNoList);
        queryWrapper1.orderByDesc(BacBloodCultrue::getCreateTime);
        List<BacBloodCultrue> lst = bacBloodCultrueMapper.selectList(queryWrapper1);
        if (ToolsUtils.isNotEmpty(lst)) {
            for (BacTestCultureMedium medium : resList) {
                List<BacBloodCultrue> collect = lst.stream().filter(a -> a.getCulMediumNo().equals(medium.getCultureNo())).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(collect)) {
                    BacBloodCultrue bacBloodCultrue = collect.get(0);
                    String result = bacBloodCultrue.getResult();
                    Double isAlarm = bacBloodCultrue.getIsAlarm();
                    //将isAlarm设置到medium的isAlarm字段上
                    medium.setIsAlarm(Integer.valueOf(isAlarm.intValue()));
                    //是否是阳性  判断result是否等于阳性
                    if ("阳性".equals(result)) {
                        medium.setIsPositive(1);
                    } else {
                        medium.setIsPositive(0);
                    }
                }
            }
        }
        List<String> ids = resList.stream().map(BacTestCultureMedium::getId).collect(Collectors.toList());


        MPJLambdaWrapper<BacTestCultureMedium> queryChildrenWrapper = new MPJLambdaWrapper<>();
        queryChildrenWrapper.selectAll(BacTestCultureMedium.class);
        queryChildrenWrapper.selectAs("tb1", BacCultureMedium::getMinCollection, BacTestCultureMedium::getMinBloodVal);
        queryChildrenWrapper.selectAs("tb1", BacCultureMedium::getWeight, BacTestCultureMedium::getCultuMedWeight);
        queryChildrenWrapper.selectAs("tb1", BacCultureMedium::getContainerBarcodeHead, BacTestCultureMedium::getCultuMedBarCodePrefix);
        queryChildrenWrapper.selectAs("tb1", BacCultureMedium::getContainerBarcodeLength, BacTestCultureMedium::getCultuMedBarCodeLength);
        queryChildrenWrapper.selectAs("tb1", BacCultureMedium::getMediumType, BacTestCultureMedium::getMediumType);
        queryChildrenWrapper.selectAs("tb1", BacCultureMedium::getMediumColor, BacTestCultureMedium::getMediumColor);
        queryChildrenWrapper.selectAs("tb1", BacCultureMedium::getMediumTag, BacTestCultureMedium::getMediumTag);
        queryChildrenWrapper.selectAs("tb1", BacCultureMedium::getCultureBottle, BacTestCultureMedium::getCultureBottle);
        queryChildrenWrapper.and(p -> p.isNotNull(BacTestCultureMedium::getParentId).ne(BacTestCultureMedium::getParentId, ""));
        queryChildrenWrapper.eq(BacTestCultureMedium::getDelFlag, 0);
        queryChildrenWrapper.eq(StringUtils.isNotBlank(input.getReqNo()), BacTestCultureMedium::getReqNo, input.getReqNo());
        queryChildrenWrapper.in(BacTestCultureMedium::getParentId, ids);
        queryChildrenWrapper.leftJoin(BacCultureMedium.class, "tb1", p -> p.eq(BacCultureMedium::getCode, BacTestCultureMedium::getCultureNo));

        List<BacTestCultureMedium> resChildrenList = bacTestCultureMediumMapper.selectList(queryChildrenWrapper);

        for (BacTestCultureMedium m : resList) {
            if (resChildrenList.isEmpty()) {
                continue;
            }
            List<BacTestCultureMedium> childrens = resChildrenList.stream().filter(p -> StringUtils.isNotBlank(p.getParentId()) && p.getParentId().equals(m.getId())).collect(Collectors.toList());
            if (!childrens.isEmpty()) {
                m.setChildren(childrens);
            }
        }


        return Result.succ(1, "成功", resList);
    }

    @Autowired
    BacBloodCultrueMapper bacBloodCultrueMapper;
}
